/*
 * RelationTable.java abstract class for relation tables in the database 
 *  'context' (copy of WebDeso).
 *
 * Copyright (c) 2007 Computer-Aided Integrated Systems Laboratory, 
 * St. Petersburg Institute for Informatics and Automation RAS http://cais.lisa.iias.spb.su
 */

package dbcontext;

/** Abstract class for relation tables (hierarchy, associative and 
 * class-attribute) in the database (context or webdeso).
 */
abstract class RelationTable {
    
    /** Relation's identifier, it equals class_attr.id, or hierarchy.id, 
     * or acon.id in database */
    public int      id;
    
    private int     relation_type;
    
    /** Gets relation (attr-class, hier, or associations) from webdeso database, 
     * stores it to context database.
     * 
     * @param relation_type  type of relation/constraint: 0 - attribute 
     *                      belonging to class, 1 - hierarchy, 2 - associated
     */ 
    public static RelationTable getByMovingFromWebdesoToContext(
            java.sql.Connection webdeso_conn, java.sql.Connection context_conn, 
            int relation_type, int relation_id_in_webdeso) {
        
        if(0==relation_type) { // 0 attribute-class relations
            //ClassAttrTable ca_context, ca_webdeso;
            
            //ca_webdeso = ClassAttrTable.getByID(webdeso_conn, relation_id_in_webdeso);
            //if(null == ca_webdeso) {
            //    System.out.println("Error in context.RelationTable.java getByMovingFromWebdesoToContext()):: "+
            //            "relation_type=0 (class_attr). Error in webdeso data: func_constr.relation_id="+relation_id_in_webdeso+
            //        " points to webdeso.class_attr.id which is absent!");
            //    return null;
            //}
            //int class_id = ca_webdeso._class.class_id;
            //int attr_id  = ca_webdeso. attr. attr_id;
                    
            //ClassAttrTable.insert(context_conn, class_id, attr_id);
            //ca_context = ClassAttrTable.get(context_conn, class_id, attr_id);
            // ca_context.id is id in context
            
            
            ClassTable c_context, c_webdeso;
            c_webdeso = ClassTable.getByClassID(webdeso_conn, relation_id_in_webdeso);
            if(null == c_webdeso) {
                System.out.println("Error in context.RelationTable.java getByMovingFromWebdesoToContext()):: "+
                    "relation_type=0 (class). Error in webdeso data: func_constr.relation_id="+relation_id_in_webdeso+
                    " points to webdeso.class.id which is absent!");
                return null;
            }
            ClassTable.insert(context_conn, c_webdeso.term.lang, c_webdeso.class_id, 
                    c_webdeso.parent_id, c_webdeso.onto_id, c_webdeso.base_onto_id, 
                    c_webdeso.term.text, c_webdeso.term.desc);
            c_context = ClassTable.getByClassID(context_conn, c_webdeso.class_id);
            // c_context.id is the same ID in context.class and in webdeso.class
            
            return c_context;
        } else
            if (1==relation_type) { // 1 hierarchy
            HierarchyTable hier_webdeso = HierarchyTable.getByID(webdeso_conn, relation_id_in_webdeso);
            
            int parent_class_id = hier_webdeso.parent_class.class_id;
            int  child_class_id = hier_webdeso.child_class. class_id;
            HierarchyTable.insert(context_conn, parent_class_id, child_class_id);
            return HierarchyTable.get(context_conn, parent_class_id, child_class_id);
        } else 
            if (2==relation_type) { // 2 associative
            AssociativeTable assoc_webdeso = AssociativeTable.getByID(webdeso_conn, relation_id_in_webdeso);
            
            int class1_id = assoc_webdeso.class1.class_id;
            int class2_id = assoc_webdeso.class2.class_id;
            AssociativeTable.insert(context_conn, class1_id, class2_id);
            return AssociativeTable.get(context_conn, class1_id, class2_id);
        }
        
        System.out.println("Error in context.RelationTable.java getByMovingFromWebdesoToContext()):: relation_type<>0,1,2, relation_type="+relation_type+
                    " (0 - attribute belonging to class, 1 - hierarchy, 2 - associated), relation_id_in_webdeso="+relation_id_in_webdeso);
        return null;
    }
    
    
    /** Gets relation (attr-class, hier, or associations) from database.
     * 
     * @param relation_type  type of relation/constraint: 0 - attribute 
     *                      belonging to class, 1 - hierarchy, 2 - associated
     */ 
    public static RelationTable getRelation (java.sql.Connection conn,
                                    int relation_type, int relation_id)
    {
        if     (0==relation_type) {
            return ClassTable. getByClassID(conn, relation_id);
        } else 
            if (1==relation_type) {
            return HierarchyTable.  getByID(conn, relation_id);
        } else 
            if (2==relation_type) {
            return AssociativeTable.getByID(conn, relation_id);
        }
        System.out.println("Error in context.RelationTable.java get()):: relation_type<>0,1,2, relation_type="+relation_type+
                            " (0 - attribute belonging to class, 1 - hierarchy, 2 - associated), relation_id="+relation_id);
        return null;
    }
}
